package com.laizhenghua.hotel.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.laizhenghua.hotel.entity.UsersEntity;
import com.laizhenghua.hotel.service.UsersService;
import com.laizhenghua.hotel.utils.MD5;
import com.laizhenghua.hotel.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: 处理管理员请求
 * @author: laizhenghua
 * @date: 2021/3/13 20:40
 */
@Controller
@RequestMapping(path = "/hotel/manage")
public class ManageController {
    @Autowired
    private UsersService usersService;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @GetMapping(path = "/reg")
    public String toRegisterPage(){
        return "admin/register";
    }

    @GetMapping(path = "/login")
    public String toLoginPage(){
        return "admin/login";
    }

    @PostMapping("/doLogin")
    @ResponseBody
    public R doLogin(@RequestBody Map<String,Object> params) {
        System.out.println("username: " + params.get("username"));
        System.out.println("password: " + params.get("password"));
        UsersEntity usersEntity = usersService.userLogin(params.get("username").toString(), MD5.MD51(params.get("password").toString()));
        HashMap<String, Object> map = new HashMap<>();
        map.put("in_login",true);
        if(usersEntity != null) {
            map.put("user",usersEntity);
            ValueOperations<String, Object> ops = redisTemplate.opsForValue();
            ops.set("user_"+usersEntity.getId(),usersEntity);
            map.put("url","hotel/manage/index");
            map.put("state","success");
            map.put("message","恭喜您登陆成功");
        }else {
            map.put("url","hotel/manage/login");
            map.put("state","error");
            map.put("message","账号或者密码错误请重试");
        }
        return R.ok().put("login_info",map);
    }

    /**
     * 跳转至管理员首页
     */
    @GetMapping("/index")
    public String toAdminIndexPage(@RequestParam("user_id") String user_id, Model model) {
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        Object object = ops.get("user_" + user_id);
        if(object != null) {
            UsersEntity usersEntity = (UsersEntity) object;
            // System.out.println(usersEntity);
            model.addAttribute("user_info",usersEntity);
            return "admin/index";
        }
        return "redirect:login";
    }
    /**
     * 获取后台管理员信息
     * API: http://localhost:8080/hotel/manage/index/user_info
     * 请求参数：user_id
     * 响应数据：userEntity对象
     */
    @GetMapping("index/user_info/{user_id}")
    @ResponseBody
    public R getAdminInfo(@PathVariable("user_id") Integer user_id){
        System.out.println("user_id: " + user_id);
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        UsersEntity usersEntity = (UsersEntity) ops.get("user_" + user_id);
        HashMap<String, Object> map = new HashMap<>();
        if(usersEntity == null){
            map.put("url","hotel/manage/login");
            map.put("state","error");
            map.put("message","请先登录在进入后台");
            return R.ok().put("data",map);
        }
        map.put("user",usersEntity);
        map.put("state","success");
        return R.ok().put("data",map);
    }
}
